package com.ruoyi.platform.system.user.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.base.page.PageData;
import com.ruoyi.base.permission.TokenManager;
import com.ruoyi.base.service.BaseService;
import com.ruoyi.platform.system.role.dao.UserRoleDAO;
import com.ruoyi.platform.system.role.manager.RoleManager;
import com.ruoyi.platform.system.role.pojo.UserRoleDO;
import com.ruoyi.platform.system.user.dao.UserDAO;
import com.ruoyi.platform.system.user.manager.UserManager;
import com.ruoyi.platform.system.user.pojo.UserDO;
import com.ruoyi.platform.system.user.pojo.UserDTO;
import com.ruoyi.platform.system.user.pojo.UserEditQuery;
import com.ruoyi.platform.system.user.pojo.UserListDTO;
import com.ruoyi.platform.system.verify.manager.VerifyCodeManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-13 05:40:22
 */
@Slf4j
@Service
public class UserService extends BaseService {
    @Autowired
    private UserDAO sysUserDao;
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private UserManager userManager;
    @Autowired
    private VerifyCodeManager verifyCodeManager;
    @Autowired
    private UserRoleDAO userRoleDAO;
    @Autowired
    private RoleManager roleManager;

    /**
     * 获取用户列表
     */
    public PageData<UserDO, UserListDTO> getUserPlatList(int pageNum, int pageSize, String nickName) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserDO> userList = sysUserDao.selectPlatList(nickName);

        if (CollectionUtils.isEmpty(userList)) {
            return new PageData<>(userList, UserListDTO::new);
        }

        List<Long> userIdList = userList.stream().map(UserDO::getUserId).collect(Collectors.toList());
        List<UserRoleDO> userRoleList = userRoleDAO.selectByUserIdList(userIdList);
        return new PageData<>(userList, x -> new UserListDTO(x, userRoleList));
    }

    /**
     * 获取用户详情
     */
    public UserDTO getUserDetail(long userId) {
        UserDO user = userManager.checkUserId(userId);
        List<UserRoleDO> userRoleList = userRoleDAO.selectByUserIdList(Collections.singletonList(userId));
        return new UserDTO(user, userRoleList);
    }

    /**
     * 修改用户
     */
    @Transactional(rollbackFor = Exception.class)
    public void editUser(long userId, UserEditQuery query, List<Integer> roleCodeList) {
        userManager.checkUserId(userId);
        //检查手机号是否存在
        userManager.checkPhoneExist(userId, query.getPhone());

        sysUserDao.updateUserByPrimaryKeySelective(query.convert(userId));
        roleManager.setUserRole(userId, roleManager.checkCode(roleCodeList));

        //刷新登陆缓存
        tokenManager.refreshCache(tokenManager.getLoginUserId());
    }

    /**
     * 修改我的信息
     */
    public void editUser(UserEditQuery query) {
        long userId = tokenManager.getLoginUserId();
        //检查手机号是否存在
        userManager.checkPhoneExist(userId, query.getPhone());

        sysUserDao.updateUserByPrimaryKeySelective(query.convert(userId));

        //刷新登陆缓存
        tokenManager.refreshCache(userId);
    }

    /**
     * 修改我的手机号码
     */
    public void editMyPhone(String phone, String code) {
        //检查验证码
        verifyCodeManager.checkPhoneVerifyCode(phone, code);
        //检查手机号是否存在
        long userId = tokenManager.getLoginUserId();
        userManager.checkPhoneExist(userId, phone);

        UserDO user = new UserDO();
        user.setUserId(userId);
        user.setPhone(phone);
        sysUserDao.updateUserByPrimaryKeySelective(user);

        //刷新登陆缓存
        tokenManager.refreshCache(userId);
    }

    /**
     * 修改账号状态
     */
    public void editAccountStatus(long userId, int accountStatus) {
        userManager.checkSystemUser(userId);
        userManager.checkUserId(userId);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(userId, accountStatus));

        //刷新登陆缓存
        tokenManager.refreshCache(userId);
    }

    /**
     * 删除用户
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(long userId) {
        userManager.checkSystemUser(userId);

        sysUserDao.delUserByPrimaryKey(userId);
        userRoleDAO.delByUserId(userId);

        //刷新登陆缓存
        tokenManager.refreshCache(userId);
    }
}